<template>
		<el-container>
				<el-header style="height:auto" class="nopadding_lf">
					<div class="left-panel">
						<el-button type="primary" icon="el-icon-plus" @click="add" plain>添加</el-button>
					</div>
					<div class="right-panel">
						<scSearch @upsearch="upsearch" >
								<template #item>
								<el-form-item label="用户名称：">
									<el-input v-model="search.userName" placeholder="用户名称" @keyup.enter="upsearch" clearable></el-input>
								</el-form-item>
								<el-form-item label="手机号码">
									<el-input v-model="search.phonenumber" placeholder="手机号码" @keyup.enter="upsearch" clearable></el-input>
								</el-form-item>
								<el-form-item label="状态：">
								<el-select v-model="search.status" @change="upsearch">
									<el-option :value="0" label="正常"></el-option>
									<el-option :value="1" label="停用"></el-option>
								</el-select>
							</el-form-item>
							<!-- <el-form-item label="创建时间：" >
								<el-date-picker v-model="creatDate" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" value-format="YYYY-MM-DD"  @change="dateChange" ></el-date-picker>
							</el-form-item> -->
								</template>
						</scSearch>
					</div>
				</el-header>
				<el-main class="nopadding">
					<scTable ref="table" :apiObj="apiObj"  :params="search" @selection-change="selectionChange" stripe remoteSort remoteFilter>
						<el-table-column type="selection" width="50" align="center" />
						<el-table-column label="用户编号" prop="userId" width="120"/>
						<el-table-column label="用户名称" prop="userName" />
						<el-table-column label="用户昵称" prop="nickName" />
						<el-table-column label="部门"  prop="dept.deptName" />
						<el-table-column label="手机号码"  prop="phonenumber"  width="120" />
						<el-table-column label="状态" prop="status" width="80">
							<template #default="scope">
								<el-switch v-model="scope.row.status" @click="changeSwitch($event, scope.row)" :loading="scope.row.$switch_status" :active-value="'0'" :inactive-value="'1'"></el-switch>
							</template>
						</el-table-column>
						<el-table-column label="创建时间"  prop="createTime" width="160">
							<template #default="scope">
								<span>{{ this.$TOOL.dateFormat(scope.row.createTime) }}</span>
							</template>
						</el-table-column>

						<el-table-column label="操作" fixed="right" align="center" width="180">
							<template #default="scope">
								<el-button-group>
									<el-button text type="primary" size="small" @click="table_edit(scope.row)">编辑</el-button>
									<el-button text type="primary" size="small" @click="table_reset(scope.row)">重置密码</el-button>
									<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
										<template #reference>
											<el-button text type="danger" size="small">删除</el-button>
										</template>
									</el-popconfirm>
								</el-button-group>
							</template>
						</el-table-column>
					</scTable>
				</el-main>
		</el-container>

	<!-- 新增/编辑 -->
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></save-dialog>
	<!-- 重置密码 -->
	<reset-pass v-if="dialog.resetPass" ref="resetDialog" @success="handleSaveSuccess" @closed="dialog.resetPass=false"></reset-pass>
	<!-- 分配角色 -->
	<!-- <edit-role v-if="dialog.editRole" ref="editRoleDialog" @success="handleSaveSuccess" @closed="dialog.editRole=false"></edit-role> -->
</template>

<script>
	import saveDialog from './save'
	import resetPass from './resetPass'
	// import editRole from './editRole'

	export default {
		name: 'user',
		components: {
			saveDialog,
			resetPass,
			// editRole
		},
		data() {
			return {
				dialog: {
					save: false,
					resetPass: false,
					editRole: false,
				},
				creatDate: [],
				apiObj: this.$API.auth.getUserList,
				selection: [],
				search: {},
			}
		},
		mounted() {
			this.reset()
		},
		methods: {
			//添加
			add(){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open().setData()
				})
			},
			//编辑
			table_edit(row){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('edit').setData(row)
				})
			},
			// 重置密码
			table_reset(row) {
					this.dialog.resetPass = true
					this.$nextTick(() => {
						this.$refs.resetDialog.open().setData(row)
					})
			},
			// 选择创建时间
			dateChange(val) {
				this.$TOOL.addDateRange(this.search, val)
				this.upsearch()
			},
			//表单注入数据
			async setData(data){
				this.reset()
				let search = this.search
				search.deptId= data.id
				this.$refs.table.upData(search)
			},
			//删除
			async table_del(row, index){
				var reqData = {userId: row.userId}
				var res = await this.$API.auth.editUser.delete(reqData);
				if(res.code == 200) {
					this.$message.success("删除成功")
					this.$refs.table.upData()
				}
			
			},
			//修改状态
			async changeSwitch(val, row){
				row.status = row.status == 0 ? 1 : 0
				row.$switch_status = 1;
				let changeStatus =  row.status == 0 ? 1 : 0
				var reqData = {
					userId: row.userId,
					status: changeStatus
				}
				var res = await this.$API.auth.editUserStatus.put(reqData);
				if(res.code == 200) {
					delete row.$switch_status;
					row.status =  row.status == 0 ? 1 : 0
					this.$message.success("操作成功")
					this.$refs.table.upData()
				}
				
			},
			//表格选择后回调事件
			selectionChange(selection){
				this.selection = selection;
			},

			// 表单重置
			reset() {
				this.search = {
					userName: '',
					phonenumber: '',
					status: null,
					deptId: null
				}
			},

			//搜索
			upsearch(type){
				let search = this.search
				if(type == 'clear') {
					this.reset()
				}
				this.$refs.table.upData(search)
			},
			//本地更新数据
			handleSaveSuccess(data, mode){
				this.$refs.table.refresh()
			},
		}
	}
</script>

<style lang="scss" scoped>
.auth_item {
	display: flex;
	flex-wrap: wrap;
	margin-bottom: 4px;
	.tagsList_ul .el-tag{
		margin-right: 4px;
		margin-bottom: 4px;

	}
}
</style>
